package com.hilon.aixinmis1.base.flow;

import com.hilon.foundation.base.BaseServiceImpl;
import com.hilon.foundation.util.HilonTools;
import com.hilon.foundation.util.ResultBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class FlowControlServiceImpl
        extends BaseServiceImpl<FlowControl, FlowControl>
        implements FlowControlService {
    public FlowControlServiceImpl(EntityManager entityManager) {
        super(entityManager);
        this.entityName = "FlowControl";
        this.viewName = "FlowControl";
    }


    @Transactional
    public ResultBean<String> deleteByIds(List<Long> ids) {
        String idsStr = HilonTools.getIdsStr(ids);

        String sql = "update " +
                entityName +
                " e set e.isDeleted=true " +
                "where e.id in (" + idsStr + ") ";
        try {
            Query query = entityManager.createQuery(sql);
            int i = query.executeUpdate();
            if (i > 0)
                return new ResultBean<>(200, "删除成功");
            else
                return new ResultBean<>(501, "删除失败,该记录已审核");
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "删除程序失败");
        }
    }

    /**
     * @param entityName
     * @param idReceipt
     * @return
     */
    @Override
    public ResultBean<List<FlowControl>> getFlowControlsByEntityNameAndId(String entityName, Long idReceipt) throws Exception {
        String sql = "select e from FlowControl e " +
                "where e.entityName = ?1 " +
                "and e.idReceipt = ?2 ";
        TypedQuery<FlowControl> query = entityManager.createQuery(sql, FlowControl.class);
        query.setParameter(1, entityName);
        query.setParameter(2, idReceipt);
        List<FlowControl> list = query.getResultList();
        return new ResultBean<>(list);
    }

    @Override
    public ResultBean<List<FlowControlModel>> getFlowControlModels(String entityName,
                                                                   String categoryType,
                                                                   String categoryTypeDetail) throws Exception {
        System.out.println(entityName);
        String sql = "select e from FlowControlModel e " +
                "where e.entityName = ?1 " +
                "and e.categoryType = ?2 " +
                "and e.categoryTypeDetail = ?3 ";
        TypedQuery<FlowControlModel> query = entityManager.createQuery(sql, FlowControlModel.class);
        query.setParameter(1, entityName);
        query.setParameter(2, categoryType);
        query.setParameter(3, categoryTypeDetail);
        List<FlowControlModel> list = query.getResultList();
        System.out.println(list);
        return new ResultBean<>(list);
    }

    /**
     * @param flowControls
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResultBean<String> saveAll(List<FlowControl> flowControls) throws Exception {
        for (int i = 0; i < flowControls.size(); i++) {
            FlowControl child = flowControls.get(i);
            child.setId(null);
            entityManager.persist(child); // 非 merge
            if (i % 20 == 0) {
                entityManager.flush(); // 分批刷入
                entityManager.clear(); // 清理一级缓存
            }
        }
        entityManager.flush(); // 分批刷入
        entityManager.clear(); // 清理一级缓存
        return new ResultBean<>("成功保存流程设定数据");
    }

    /**
     * @param flowControlModels
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResultBean<String> saveAsModel(List<FlowControlModel> flowControlModels) throws Exception {
        for (int i = 0; i < flowControlModels.size(); i++) {
            FlowControlModel child = flowControlModels.get(i);
            child.setId(null);
            entityManager.persist(child); // 非 merge
            if (i % 20 == 0) {
                entityManager.flush(); // 分批刷入
                entityManager.clear(); // 清理一级缓存
            }
        }
        entityManager.flush(); // 分批刷入
        entityManager.clear(); // 清理一级缓存
        return new ResultBean<>("成功保存流程设定模板");
    }
}
